package com.shwread.android.qysw.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shwread.android.qysw.activity.OrderDetailActivity;
import com.shwread.android.qysw.activity.OrderPayActivity;
import com.shwread.android.qysw10000209.R;
import com.shwread.android.ui.customview.MyListView;
import com.squareup.picasso.Picasso;

import java.util.List;

import logic.bean.HotelOrdersBean;
import logic.constants.DefaultConsts;
import logic.util.MoneyUtil;
import logic.util.Util;
import logic.util.Utils;

/**
 * author: lexy
 * email : hpxu2004@163.com
 * date  : 2016-07-22
 * time  : 14:46
 */
public class ContentAdapter
        extends
        BaseAdapter {

    private Context context;
    private List<HotelOrdersBean.ReturnObjectBean.HotelOrderListBean> list = null;

    public ContentAdapter(Context context, List<HotelOrdersBean.ReturnObjectBean.HotelOrderListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size() <= 0 ? 0 : list.size();
        //			return (goodsList==null||goodsList.size()==0)?0:goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return (list == null || list.size() == 0) ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.my_goods_list_item, null);
            viewHolder.tvOrderNumber = (TextView) convertView.findViewById(R.id
                    .goods_detail_item_top_order_number);
            viewHolder.tvOrderState = (TextView) convertView.findViewById(R.id
                    .goods_detail_item_top_order_state);
            viewHolder.lvOrderView = (MyListView) convertView.findViewById(R.id.my_goods_list_item_listview);
            viewHolder.tvGoodsTotalPrice = (TextView) convertView.findViewById(R.id
                    .goods_detail_item_bottom_total_pay_tv);
            viewHolder.butGoodsClose = (Button) convertView.findViewById(R.id
                    .goods_detail_item_bottom_close_btn);
            viewHolder.butGoodsComment = (Button) convertView.findViewById(R.id
                    .goods_detail_item_bottom_comment_btn);
            viewHolder.butGoodsDeliver = (Button) convertView.findViewById(R.id
                    .goods_detail_item_bottom_deliver_btn);
            viewHolder.butGoodsPay = (Button) convertView.findViewById(R.id.goods_detail_item_bottom_pay_btn);
            viewHolder.btn_qrcode = (Button) convertView.findViewById(R.id.btn_qrcode);
            viewHolder.rlayTotalMoney = (RelativeLayout) convertView.findViewById(R.id
                    .goods_detail_total_money_rlay);
            viewHolder.llayCloseCause = (LinearLayout) convertView.findViewById(R.id.deal_close_cause_llay);
            viewHolder.tvCloseCause = (TextView) convertView.findViewById(R.id.deal_close_cause_tv);
            viewHolder.butGoodsComment.setVisibility(View.GONE);
            viewHolder.butGoodsClose.setVisibility(View.GONE);
            viewHolder.butGoodsDeliver.setVisibility(View.GONE);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final HotelOrdersBean.ReturnObjectBean.HotelOrderListBean orderDetailInfo = (HotelOrdersBean
                .ReturnObjectBean.HotelOrderListBean) getItem(position);
        viewHolder.tvOrderNumber.setText("订单号:" + orderDetailInfo.getOrderNo());
        viewHolder.tvOrderNumber.setVisibility(View.VISIBLE);
        viewHolder.tvGoodsTotalPrice.setText(" ¥" + Utils.getMoneyFormat(MoneyUtil.divide(orderDetailInfo
                .getTotalMoney(), 100)));
        //设置状态
        viewHolder.tvOrderState.setVisibility(View.VISIBLE);
        //如果订单状态为 5 即关闭    显示关闭原因   隐藏订单总价    否则隐藏关闭原因 显示其它状态
        if (Integer.parseInt(orderDetailInfo.getStatus()) == 5) {
            viewHolder.llayCloseCause.setVisibility(View.VISIBLE);
            viewHolder.rlayTotalMoney.setVisibility(View.GONE);
        } else {
            viewHolder.llayCloseCause.setVisibility(View.GONE);
            viewHolder.rlayTotalMoney.setVisibility(View.VISIBLE);
        }
        switch (Integer.parseInt(orderDetailInfo.getStatus())) {
//            case 0:
//                viewHolder.tvOrderState.setText("待付款");
//                viewHolder.butGoodsPay.setVisibility(View.VISIBLE);
//                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
//                break;
            case 1:
                viewHolder.tvOrderState.setText("待付款");
                viewHolder.butGoodsPay.setVisibility(View.VISIBLE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                break;
            case 2:
                //					viewHolder.tvOrderState.setText("待收货");
                viewHolder.tvOrderState.setText("已付款");
                viewHolder.butGoodsPay.setVisibility(View.GONE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                break;
            case 3:
                viewHolder.tvOrderState.setText("已完成");
                viewHolder.butGoodsClose.setVisibility(View.GONE);
                viewHolder.butGoodsPay.setVisibility(View.GONE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                break;
            case 4:
                viewHolder.tvOrderState.setText("交易成功");
                viewHolder.butGoodsPay.setVisibility(View.GONE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                break;
            case 5:
                viewHolder.tvOrderState.setText("交易关闭");
                viewHolder.butGoodsClose.setVisibility(View.GONE);
                viewHolder.butGoodsPay.setVisibility(View.GONE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                viewHolder.tvCloseCause.setText("关闭原因：买家付款超时");
                break;
            case 6:
                viewHolder.tvOrderState.setText("已退房");
                viewHolder.butGoodsClose.setVisibility(View.GONE);
                viewHolder.butGoodsPay.setVisibility(View.GONE);
                viewHolder.butGoodsDeliver.setVisibility(View.GONE);
                break;
            default:
                break;
        }

        viewHolder.butGoodsClose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.showToast(context, "您点击了关闭");
            }
        });

        viewHolder.butGoodsComment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.showToast(context, "您点击了评论");
            }
        });

        viewHolder.butGoodsDeliver.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.showToast(context, "您点击了物流");
                //					Intent intent = new Intent(mContext, GoodsOrderDeliverDetailActivity
                // .class);
            }
        });

        viewHolder.butGoodsPay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
//                isRefresh = true;
                //					Utils.showToast(mContext, "您点击了付款");
                Intent intent = new Intent(context, OrderPayActivity.class);
                //支付总金额 需要传递过去
                intent.putExtra(DefaultConsts.ORDER_DETAIL_ORDER_SERIAL, orderDetailInfo.getOrderNo());
                intent.putExtra("orderId", orderDetailInfo.getId()+"");
                intent.putExtra(DefaultConsts.ORDER_DETAIL_ORDER_PAY_MONEY, Double.parseDouble(orderDetailInfo.getTotalMoney()+""));
                intent.putExtra(DefaultConsts.ORDER_DETAIL_ORDER_ORDER_TITLE, orderDetailInfo.getHotelName());
                intent.putExtra("type",40);
//                intent.putExtra(DefaultConsts.ORDER_DETAIL_ORDER_ORDER_INTRO, orderDetailInfo.getHotelName());
                context.startActivity(intent);
            }
        });

        SubOrderAdapter subOrderAdapter = new SubOrderAdapter(orderDetailInfo);
        viewHolder.lvOrderView.setAdapter(subOrderAdapter);
        subOrderAdapter.notifyDataSetChanged();

        viewHolder.lvOrderView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HotelOrdersBean.ReturnObjectBean.HotelOrderListBean Info = orderDetailInfo;
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra(DefaultConsts.ORDER_DETAIL_FROM_TYPE, DefaultConsts.ORDER_DETAIL_STATE_ALL);
                intent.putExtra(DefaultConsts.ORDER_DETAIL_ORDER_ID, Info.getId());
                intent.putExtra("type",40);
                context.startActivity(intent);

            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView tvOrderNumber, tvOrderState;
        private MyListView lvOrderView;
        private TextView tvGoodsTotalPrice;
        private Button butGoodsDeliver;
        private Button butGoodsComment;
        private Button butGoodsClose;
        private Button butGoodsPay, btn_qrcode;
        private RelativeLayout rlayTotalMoney;
        private LinearLayout llayCloseCause;
        private TextView tvCloseCause;

    }

    class SubOrderAdapter
            extends
            BaseAdapter {

        private HotelOrdersBean.ReturnObjectBean.HotelOrderListBean orderInfo;

        /**
         *
         */
        public SubOrderAdapter(HotelOrdersBean.ReturnObjectBean.HotelOrderListBean info) {
            this.orderInfo = info;
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int position) {

            return orderInfo;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SubOrderViewHolder holder;
            if (convertView == null) {
                holder = new SubOrderViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.goods_detail_item, null);
                holder.ivGoodsCover = (ImageView) convertView.findViewById(R.id.goods_detail_item_image);
                holder.tvGoodsIntro = (TextView) convertView.findViewById(R.id.goods_detail_item_name_tv);
                holder.tvGoodsPrice = (TextView) convertView.findViewById(R.id
                        .goods_detail_item_original_price_tv);
                holder.tvGoodsCount = (TextView) convertView.findViewById(R.id
                        .goods_detail_item_original_count_tv);
                holder.tvGoodsSort = (TextView) convertView.findViewById(R.id.goods_detail_item_classify_tv);
                holder.tvOrderListProperty = (TextView) convertView.findViewById(R.id.tvOrderListProperty);
                holder.tvTotalMoney = (TextView) convertView.findViewById(R.id.tvTotalMoney);

                convertView.setTag(holder);
            } else {
                holder = (SubOrderViewHolder) convertView.getTag();
            }


            if (!Util.isEmpty(orderInfo.getHotelPic()))
                Picasso.with(context).load(orderInfo.getHotelPic()).error(R.drawable.atlas_default_img)
                        .placeholder(R.drawable.atlas_default_img).into(holder.ivGoodsCover);
            else holder.ivGoodsCover.setImageResource(R.drawable.atlas_default_img);
            holder.tvGoodsIntro.setText(orderInfo.getHotelName());
            holder.tvGoodsPrice.setText("酒店：" + orderInfo.getRoomName());
            holder.tvGoodsCount.setText("x " + orderInfo.getAmount());
            holder.tvOrderListProperty.setText("价格：￥" + Utils.getMoneyFormat(MoneyUtil.divide(orderInfo
                    .getRoomPrice(), 100)));
            holder.tvGoodsPrice.setTextColor(Color.parseColor("#8c8c8c"));
            holder.tvOrderListProperty.setTextColor(Color.parseColor("#8c8c8c"));
//            holder.tvTotalMoney.setTextColor(Color.parseColor("#8c8c8c"));
            holder.tvTotalMoney.setVisibility(View.GONE);

            //			Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            //			String str = null, str1 = null, str2 = null;
            //			while (it.hasNext())
            //			{
            //				Map.Entry<String, String> entry = it.next();
            //				str1 = entry.getKey();
            //				str2 = entry.getValue();
            //				str = str + str1 + ":" + str2 + ",";
            //			}
            //			str = str.substring(4, (str.length() - 1)); //去掉最前面的null 和最后的“，”
            //			holder.tvGoodsSort.setText(str);
//            StringBuilder str = new StringBuilder();
//            if (orderDetailColum.getSkuProp() != null && orderDetailColum.getSkuProp().size() > 0)
//            {
//                for (Map.Entry<String, String> entry : orderDetailColum.getSkuProp().entrySet())
//                {
//                    if (str.length() > 0)
//                    {
//                        str.append(",");
//                    }
//                    str.append(entry.getKey() + ": ");
//                    str.append(entry.getValue());
//                }
//            }
//            holder.tvGoodsSort.setText(str.toString());
            return convertView;
        }
    }

    class SubOrderViewHolder {
        private TextView tvGoodsIntro;
        private TextView tvGoodsPrice;
        private TextView tvGoodsCount;
        private TextView tvGoodsSort;
        private TextView tvOrderListProperty;
        private TextView tvTotalMoney;
        private ImageView ivGoodsCover;
    }

}
